package com.jiyi.activity.datainput.sm;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.SafeEvacuationBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.jiyi.widget.picselect.PicsAdapter;
import com.jiyi.widget.picshow.ImagePagerActivity;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.utils.RadioGroupUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/29.
 */

public class SafeEvacuationReadActivity extends ActivityBase {
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;
    @BindView(R.id.ivHint)
    ImageView ivHint;
    @BindView(R.id.top_layout)
    RelativeLayout topLayout;
    @BindView(R.id.rbSettingYes)
    RadioButton rbSettingYes;
    @BindView(R.id.rbSettingNo)
    RadioButton rbSettingNo;
    @BindView(R.id.rgSettingIsOK)
    RadioGroup rgSettingIsOK;
    @BindView(R.id.tvSettingProblem)
    TextView tvSettingProblem;
    @BindView(R.id.tvSettingMeasure)
    TextView tvSettingMeasure;
    @BindView(R.id.rbExitYes)
    RadioButton rbExitYes;
    @BindView(R.id.rbExitNo)
    RadioButton rbExitNo;
    @BindView(R.id.rgExitHasUse)
    RadioGroup rgExitHasUse;
    @BindView(R.id.tvExitProblem)
    TextView tvExitProblem;
    @BindView(R.id.tvExitMeasure)
    TextView tvExitMeasure;
    @BindView(R.id.rbMaterialYes)
    RadioButton rbMaterialYes;
    @BindView(R.id.rbMaterialNo)
    RadioButton rbMaterialNo;
    @BindView(R.id.rgMaterialIsOk)
    RadioGroup rgMaterialIsOk;
    @BindView(R.id.tvMaterialProblem)
    TextView tvMaterialProblem;
    @BindView(R.id.tvMaterialMeasure)
    TextView tvMaterialMeasure;
    @BindView(R.id.rbRefugeFloorsYes)
    RadioButton rbRefugeFloorsYes;
    @BindView(R.id.rbRefugeFloorsNo)
    RadioButton rbRefugeFloorsNo;
    @BindView(R.id.rgHasRefugeFloorsUse)
    RadioGroup rgHasRefugeFloorsUse;
    @BindView(R.id.tvRefugeFloorsProblem)
    TextView tvRefugeFloorsProblem;
    @BindView(R.id.tvRefugeFloorsMeasure)
    TextView tvRefugeFloorsMeasure;
    @BindView(R.id.rbRefugeFloorsAcrossYes)
    RadioButton rbRefugeFloorsAcrossYes;
    @BindView(R.id.rbRefugeFloorsAcrossNo)
    RadioButton rbRefugeFloorsAcrossNo;
    @BindView(R.id.rgHasRefugeFloorsAcross)
    RadioGroup rgHasRefugeFloorsAcross;
    @BindView(R.id.tvRefugeFloorsAcrossProblem)
    TextView tvRefugeFloorsAcrossProblem;
    @BindView(R.id.tvRefugeFloorsAcrossMeasure)
    TextView tvRefugeFloorsAcrossMeasure;

    String build_id;

    @BindView(R.id.gvSetting)
    NoScrollGridView gvSetting;
    @BindView(R.id.gvExit)
    NoScrollGridView gvExit;
    @BindView(R.id.gvMaterial)
    NoScrollGridView gvMaterial;
    @BindView(R.id.rbRefugeFloorsNoExist)
    RadioButton rbRefugeFloorsNoExist;
    @BindView(R.id.gvRefugeFloors)
    NoScrollGridView gvRefugeFloors;
    @BindView(R.id.rbRefugeFloorsAcrossNoExist)
    RadioButton rbRefugeFloorsAcrossNoExist;
    @BindView(R.id.gvRefugeFloorsAcross)
    NoScrollGridView gvRefugeFloorsAcross;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safe_evacuation_read);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        getIntentData();
        setTop();
        setViewStatus();

        if (!TextUtils.isEmpty(build_id)) {
            //修改时，获取数据
            getData();
        }
    }

    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("安全疏散与避难");
        tvSearch.setVisibility(View.GONE);
    }

    private void setViewStatus() {
        RadioGroupUtil.disableRadioGroup(rgExitHasUse);
        RadioGroupUtil.disableRadioGroup(rgHasRefugeFloorsAcross);
        RadioGroupUtil.disableRadioGroup(rgHasRefugeFloorsUse);
        RadioGroupUtil.disableRadioGroup(rgMaterialIsOk);
        RadioGroupUtil.disableRadioGroup(rgSettingIsOK);
    }

    public void getData() {
        HttpMethods.getInstance().getSafeEvacuationInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<SafeEvacuationBean>(this) {
                    @Override
                    public void onNext(SafeEvacuationBean bean) {
                        if (bean == null || "".equals(bean.getGson_null())) {
                            bean = new SafeEvacuationBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            bean.setSetting_meet_standard(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            bean.setIs_occupy(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            bean.setDecoration_meet_standard(bc3);
                            BaseCheck bc4 = new BaseCheck();
                            bc4.setIs_ok(0);
                            bc4.setProblem("");
                            bc4.setMeasures("");
                            bean.setRefuge_is_occupied(bc4);
                            BaseCheck bc5 = new BaseCheck();
                            bc5.setIs_ok(0);
                            bc5.setProblem("");
                            bc5.setMeasures("");
                            bean.setRefuge_is_crossing(bc5);
                        }

                        fillData(bean);
                    }
                });
    }

    private void fillData(SafeEvacuationBean safeEvacuationBean) {
        if (safeEvacuationBean.getSetting_meet_standard().getIs_ok() == 1) {
            rbSettingYes.setChecked(true);
        }

        tvSettingProblem.setText(safeEvacuationBean.getSetting_meet_standard().getProblem());
        tvSettingMeasure.setText(safeEvacuationBean.getSetting_meet_standard().getMeasures());

        if (safeEvacuationBean.getIs_occupy().getIs_ok() == 1) {
            rbExitYes.setChecked(true);
        }

        tvExitProblem.setText(safeEvacuationBean.getIs_occupy().getProblem());
        tvExitMeasure.setText(safeEvacuationBean.getIs_occupy().getMeasures());

        if (safeEvacuationBean.getDecoration_meet_standard().getIs_ok() == 1) {
            rbMaterialYes.setChecked(true);
        }

        tvMaterialProblem.setText(safeEvacuationBean.getDecoration_meet_standard().getProblem());
        tvMaterialMeasure.setText(safeEvacuationBean.getDecoration_meet_standard().getMeasures());

        if (safeEvacuationBean.getRefuge_is_occupied().getIs_ok() == 1) {
            rbRefugeFloorsYes.setChecked(true);
        } else if (safeEvacuationBean.getRefuge_is_occupied().getIs_ok() == 0) {
            rbRefugeFloorsNo.setChecked(true);
        }

        tvRefugeFloorsProblem.setText(safeEvacuationBean.getRefuge_is_occupied().getProblem());
        tvRefugeFloorsMeasure.setText(safeEvacuationBean.getRefuge_is_occupied().getMeasures());

        if (safeEvacuationBean.getRefuge_is_crossing().getIs_ok() == 1) {
            rbRefugeFloorsAcrossYes.setChecked(true);
        } else if (safeEvacuationBean.getRefuge_is_crossing().getIs_ok() == 0) {
            rbRefugeFloorsAcrossNo.setChecked(true);
        }

        tvRefugeFloorsAcrossProblem.setText(safeEvacuationBean.getRefuge_is_crossing().getProblem());
        tvRefugeFloorsAcrossMeasure.setText(safeEvacuationBean.getRefuge_is_crossing().getMeasures());

        initGridViewList(safeEvacuationBean);

    }

    void initGridViewList(SafeEvacuationBean safeEvacuationBean)
    {
        if (safeEvacuationBean.getSetting_meet_standard().getImage() == null || safeEvacuationBean.getSetting_meet_standard().getImage().size() <= 0) {
            gvSetting.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvSetting,safeEvacuationBean.getSetting_meet_standard().getImage());
        }


        if (safeEvacuationBean.getIs_occupy().getImage() == null || safeEvacuationBean.getIs_occupy().getImage().size() <= 0) {
            gvExit.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvExit,safeEvacuationBean.getIs_occupy().getImage());
        }

        if (safeEvacuationBean.getDecoration_meet_standard().getImage() == null || safeEvacuationBean.getDecoration_meet_standard().getImage().size() <= 0) {
            gvMaterial.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvMaterial,safeEvacuationBean.getDecoration_meet_standard().getImage());
        }

        if (safeEvacuationBean.getRefuge_is_occupied().getImage() == null || safeEvacuationBean.getRefuge_is_occupied().getImage().size() <= 0) {
            gvRefugeFloors.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvRefugeFloors,safeEvacuationBean.getRefuge_is_occupied().getImage());
        }

        if (safeEvacuationBean.getRefuge_is_crossing().getImage() == null || safeEvacuationBean.getRefuge_is_crossing().getImage().size() <= 0) {
            gvRefugeFloorsAcross.setVisibility(View.GONE);
        }
        else {
            setGridviewAdapter(gvRefugeFloorsAcross,safeEvacuationBean.getRefuge_is_crossing().getImage());
        }
    }

    void setGridviewAdapter(GridView gridView, List<String> picList)
    {
        gridView.setVisibility(View.VISIBLE);
        //图片列表
        if (picList != null) {
            final String [] urls = new String [picList.size()];
            for (int i = 0; i < picList.size(); i++) {
                urls[i] = picList.get(i);
            }

            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(view.getContext(),ImagePagerActivity.class);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls);
                    intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                    view.getContext().startActivity(intent);
                }
            });
            PicsAdapter picsAdapter = new PicsAdapter(gridView.getContext(),picList);
            gridView.setAdapter(picsAdapter);
        }
    }

    @OnClick({R.id.tvBack})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            default:
                break;
        }
    }
}
